﻿#pragma once
#include <stdint.h>
#include <iostream>
#include <fstream> 
#include <memory>
#include <string>
#include <unordered_map>

class CSyncOneMsgBinary :public std::enable_shared_from_this<CSyncOneMsgBinary>
{
public:
    CSyncOneMsgBinary(const std::string& str_folder) :m_folder(str_folder) {

    }
    ~CSyncOneMsgBinary() {

    }
    bool doRecord(int type, const char* data, int len);
private:
    std::string m_folder;
    uint64_t m_id = 0;
};
using SpSyncOneMsgBinary = std::shared_ptr<CSyncOneMsgBinary>;

class COneFixedAppendBinary : public std::enable_shared_from_this<COneFixedAppendBinary>
{
public:
    COneFixedAppendBinary(const std::string& str_floder, uint64_t max_size) :m_fodler(str_floder), m_max(max_size) {}
    ~COneFixedAppendBinary() {}
    bool doRecord(const char* data, int len);
private:
    int m_index = 0;
    std::string m_fodler;
    uint64_t m_total = 0;
    uint64_t m_max;
};
using SpOneFixedAppendBinary = std::shared_ptr<COneFixedAppendBinary>;


class CSyncMsgLength :public std::enable_shared_from_this<CSyncMsgLength>
{
public:
    CSyncMsgLength(const std::string& folder) : m_folder(folder) {
    }
    ~CSyncMsgLength() {}
    bool doRecord(int len);

    std::string m_folder;
};
using SpSyncMsgLength = std::shared_ptr<CSyncMsgLength>;

using MapSyncOneMgs = std::unordered_map<std::string, SpSyncOneMsgBinary>;
using MapSyncFixedAppendBinary = std::unordered_map < std::string, SpOneFixedAppendBinary>;
using MapSyncMsgLength = std::unordered_map<std::string, SpSyncMsgLength>;

class CNetProtoRecorder
{
public:
    CNetProtoRecorder()/* :_uid_counter(0)*/ {}
    ~CNetProtoRecorder() {}
    static CNetProtoRecorder& instance() {
        static CNetProtoRecorder ins;
        return ins;
    }
public:
    int32_t syncLoad(const char* path, char* data, int32_t max_size);
    int32_t syncSave(const char* path, const char* data, int32_t max_size);
    int32_t syncSaveAppend(const char* path, const char* data, int32_t max_size);
    std::string getRecvMsgPath(const std::string& strFolder, uint64_t uid, int type, int len);
    std::string getSendMsgPath(const std::string& strFolder, uint64_t uid, int type, int len);
    uint64_t getMsNow();

    SpSyncOneMsgBinary getOrCreateSyncOneMsgBinary(const std::string& folder);
    SpOneFixedAppendBinary getOrCreateOneFixedAppendBinary(const std::string& folder, uint64_t max);
    SpSyncMsgLength getOrCreateSyncMsgLength(const std::string& folder);
private:
    // uint64_t _uid_counter;
    MapSyncOneMgs m_mapSyncOneMsg;
    MapSyncFixedAppendBinary m_mapSyncFixedAppendBinary;
    MapSyncMsgLength m_mapSyncMsgLength;
};
#define SigNetProtoRecoder CNetProtoRecorder::instance()
